<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HtmlPrintAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;
use Illuminate\Support\Facades\Log;

class EighteenPayment extends RechargeBaseAbstract implements WAPable
{
    public function __construct()
    {
        parent::__construct();
        $this->gw_pay = 'http://yun.8au.com/API/Bank/';
        $this->gw_query = 'http://yun.8au.com/API/Bank/';
    }

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['sErrorCode'] == 1) {
                return true;
            }
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr  = 'sErrorCode=' . $data['sErrorCode'] . '&';
        $signStr .= 'bType=' . $data['bType'] . '&';
        $signStr .= 'ForUserId=' . $this->getMchId() . '&';
        $signStr .= 'LinkID=' . $data['LinkID'] . '&';
        $signStr .= 'Moneys=' . $data['Moneys'] . '&';
        $signStr .= 'AssistStr=' . $data['AssistStr'] . '&';
        $signStr .= 'keyValue=' . $this->getMchKey();
        $sign = md5(strtolower($signStr));
        if ($data['sign'] == $sign) {
            return true;
        }
        return false;

    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['LinkID'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['Moneys'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::Eighteen;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => '7772',
            PaymentMap::WX_WAP => '7771',
            PaymentMap::ALI => '888',
            PaymentMap::ALI_WAP => '999',
            PaymentMap::QQ => '555',
            PaymentMap::QQ_WAP => '666'
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('ForUserId', $this->getMchId());
        $this->setParameter('OrderId', $rechargeOrder->plat_no);
        $this->setParameter('sign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters(), 'post');
        if ($res['http_code'] == 200) {
            Log::info($res['body']);
            $resData = json_decode($res['body'], true);
            if ($resData['errcode'] == 0) {
                if ($resData['Status'] == 0) {
                    return true;
                }
            }
            return $resData['msg'];
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        $this->initParameters($rechargeOrder);
        return new HtmlPrintAction(formPostHtml($this->getGwPay(), $this->getParameters()));

    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr  = 'LinkID=' . $this->getParameter('LinkID') . '&';
        $signStr .= 'ForUserId=' . $this->getParameter('ForUserId') . '&';
        $signStr .= 'Channelid=' . $this->getParameter('Channelid') . '&';
        $signStr .= 'Moneys=' . $this->getParameter('Moneys') . '&';
        $signStr .= 'AssistStr=' . $this->getParameter('AssistStr') . '&';
        $signStr .= 'ReturnUrl=' . $this->getParameter('ReturnUrl') . '&';
        $signStr .= 'Key=' . $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "ForUserId=" . $this->getParameter('ForUserId') . '&';
        $signStr .= "OrderId=" . $this->getParameter('OrderId') . '&';
        $signStr .= "Key=" . $this->getMchKey();
        return md5($signStr);

    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement scanCode() method.
        $this->setParameter('LinkID', $rechargeOrder->plat_no);
        $this->setParameter('ForUserId', $this->getMchId());
        $this->setParameter('Channelid', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('Moneys', $rechargeOrder->order_amt);
        $this->setParameter('AssistStr', $this->setRemark($rechargeOrder));
        $this->setParameter('ReturnUrl', $this->getReturnUrl());
        $this->setParameter('NotifyUrl', $this->getCallbackUrl());
        $this->setParameter('Sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        if ($rechargeOrder->order_data['recharge_type'] == PaymentMap::ALI_WAP) {
            $this->setGwPay('https://yun.8au.com/API/Bank/');
        }
        return new HtmlPrintAction(formPostHtml($this->getGwPay(), $this->getParameters()));
    }
}
